package color;

import java.awt.Color;

/**
 * This class is used to validate an svgobjects color attributes
 */
public class ColorCheck
{
    
    SelectSVGcolors kb = new SelectSVGcolors();
    private Color   color;
    
    public Color getColor(String stroke)
    {
        // checks for #rgb format
        if (stroke.indexOf("#") >= 0 && stroke.length() == 4)
        {
            String c4 = stroke.substring(1, 2) + stroke.substring(1, 2)
                    + stroke.substring(2, 3) + stroke.substring(2, 3)
                    + stroke.substring(3, 4) + stroke.substring(3, 4);
            this.color = decodes(c4);
        }
        // check for #rrggbd format
        else if (stroke.indexOf("#") >= 0 && stroke.length() == 7)
        {
            this.color = decodes(stroke.substring(1, 6));
        }
        // check for #(rr,gg,bd) format
        else if (stroke.indexOf("(") > 0 && stroke.indexOf(",") > 0
                && stroke.indexOf("%") > 0)
        {
            
            this.color = kb.getRGB1(stroke);
            
        }
        else if (stroke.indexOf("(") > 0 && stroke.indexOf(",") > 0)
        {
            
            this.color = kb.getRGB2(stroke);
            
        }
        else
        {
            // check for color name as String
            this.color = kb.getRGB3(stroke);
            
        }
        return this.color;
    }
    
    /**
     * 
     * @return color
     */
    public Color getColor()
    {
        return color;
    }
    
    /**
     * this is a extended version parseInt class to handle errors
     * 
     * @param string
     * @return
     */
    public int parseInt(String string)
    {
        try
        {
            int kk = Integer.parseInt(string);
            if (0 > kk || kk > 255)
            {
                System.out.println("Error SVG Color out of range---"
                        + string.toString());
                System.exit(0);
            }
            
            return kk;
        }
        catch (Exception e)
        {
            System.out.println("Error in SVG file units Exiting---"
                    + string.toString());
            System.exit(0);
            
        }
        return (Integer) null;
        
    }
    
    /**
     * this function checks for rgb value range 0-255
     * 
     * @param value
     * @return value if range 0-255
     */
    public double rangecheck(double value)
    {
        if (0 > value || value > 255)
        {
            System.out.println("Error... SVG Color out of range---");
            System.exit(0);
        }
        return value;
    }
    
    /**
     * This method decode color number base to string
     * 
     * @param string
     * @return
     */
    public Color decodes(String string)
    {
        Color col = new Color(78, 255, 8);
        
        try
        {
            col = Color.decode(string);
            return col;
        }
        catch (Exception e)
        {
            System.out.println("Error! in SVG invalid Color"
                    + string.toString());
            System.exit(0);
        }
        return col;
        
    }
}
